<?php
/**
 * 用户信息
 * @copyright (c) Seer All Rights Reserved
 */

class Activity_Model {

	private $db;

	function __construct() {
		$this->db = Database::getInstance();
	}

	/**
	 * 查询活动的信息
	 * 根据 推荐值属性 0推荐
	 */
	function  selectActivity(){
		$sql = " select * from  ".DB_PREFIX."activity  where is_recommend = 0 ";
		$res = $this->db->query($sql);
		$ActivityResult = array();
		while($row = $this->db->fetch_array($res)){
			$ActivityResult[] = $row;
		}
		return $ActivityResult;
	}

	/**
	 * 查询活动对应的子活动信息 并按照sort 升序
	 */
	function  selectSubActivity($activity_id){
		$sql = "select * from ".DB_PREFIX."activity_sub where activity_id = '$activity_id' order by sort asc";
		$res = $this->db->query($sql);
		$SubActivityResult = array();
		while($row = $this->db->fetch_array($res)){
			$SubActivityResult[] = $row;
		}
		return $SubActivityResult;
	}



	/**
	 * 判断用户是否已经报名此活动
	 * @param $uid
	 * @param $activity_id
	 */
	function checkUserApply($uid,$activity_id){
		$sql = " select * from " .DB_PREFIX. "activity_apply where uid='$uid' and activity_id = '$activity_id' ";
		$res = $this->db->query($sql);
		$UserApplyResult = array();
		while($row = $this->db->fetch_array($res)){
			$UserApplyResult[] = $row;
		}
		return $UserApplyResult;
	}

	/**
	 * 查询往期活动
	 * 以最后修改时间为倒序
	 */
	function  selectOldActivity(){
		$sql = "select * from ".DB_PREFIX."activity  where is_recommend = 1 order by modify_time desc";
		$res = $this->db->query($sql);
		$OldActivityResult = array();
		while($row = $this->db->fetch_array($res)){
			$OldActivityResult[] = $row;
		}
		return $OldActivityResult;
	}

	/**
	 * 报名活动
	 */
	function applyActivity($uid,$activity_id){
		$sql = " insert into ".DB_PREFIX."activity_apply (uid,activity_id,create_time,modify_time) ".
									" values ('$uid','$activity_id',now(),now())";
		$this->db->query($sql);
		return mysql_insert_id;
	}

	/**
	 * 检索活动报名的状态
	 */
	function getActivityApplyState($uid,$activity_id){
		$sql = "select apply_state from ".DB_PREFIX."activity_apply where uid=$uid and activity_id = $activity_id";
		$res= $this->db->once_fetch_array($sql);
		return $res["apply_state"];
	}

	/**
	 * 检索活动报名的进度
	 */
	function getActivityProgressReport($uid,$activity_id){
		$sql = "select progress_report from ".DB_PREFIX."activity_apply where uid= $uid and activity_id = $activity_id";
		$res= $this->db->once_fetch_array($sql);
		return $res["progress_report"];
	}

	/**
	 * 编辑活动报名的状态
	 */
	function updateActivityApplyState($uid,$activity_id){
		$this->db->query("update ".DB_PREFIX."activity_apply set apply_state=apply_state+1 where uid=$uid and activity_id = $activity_id");
	}

	/**
	 * 检索当前关卡需要的文件数和赞数
	 */
	function getActivityNeed($activity_id,$subId){
		$sql = "select sum(production_count) as productionSum,sum(vote_count) as voteSum from".DB_PREFIX
			."activity_sub where activity_id=$activity_id and sort< = $subId";
		$res= $this->db->once_fetch_array($sql);
		return $res;
	}

	/**
	 * 修改活动进度
	 */
	function  updateActivityApplyProgressReport($uid ,$activity_id){
		$this->db->query("update ".DB_PREFIX."progress_report set progress_report=progress_report+1 where uid=$uid and activity_id = $activity_id");
	}


}
